<?php

$objXajax->registerFunction("frmhistoryinventory_loadGrid");
$objXajax->registerFunction("frmhistoryinventory_viewdetail");
include_once(CORE_PATH.'includes/summary_functions.php');
//$objXajax->registerFunction("frmhistoryinventory_type");
    
function frmhistoryinventory_viewdetail($id,$fromdate,$todate){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = frmhistoryinventory_getDetail($id,$fromdate,$todate);
    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/cus_historyinventory_detail_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmhistoryinventory_divDetail_grid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmhistoryinventory_viewdetail_tbl',true);");
    $objResponse->addScript("$('#frmhistoryinventory_divDetail').dialog('open')");
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}



function  frmhistoryinventory_getDetail($id,$fromdate,$todate){
    global $objDbSelect,$lang,$tbl_postfix,$user_id,$hotel_id;
    $now = date('Y-m-d');
    $arr=array(); 
    if($fromdate!=$now){
       $sql = "SELECT p.name_vi as name,num_start, num_end, num_in, num_out, date_sum 
       from cus_summary_product as s left join cus_product$tbl_postfix  as p on s.product_id = p.id and s.service_id = $hotel_id
        where product_id = $id and datediff('$fromdate',date_sum)<=0 and 
       datediff('$todate',date_sum)>=0 and s.service_id = $hotel_id order by date_sum";
       //echo  $sql; die();
       $arr=$objDbSelect->GetArray($sql);
       if($todate==$now){
        $sqlnew = "SELECT name_vi as name,p.quanlity as num_start,p.id,sum(num) as num_out  , (select sum(i.quanlity)  
                from  cus_importproduct as i
                where i.product_id = p.id and datediff(i.date_created,'$todate')=0 and service_id = $hotel_id
                GROUP BY i.product_id)  as num_in FROM cus_product$tbl_postfix as p
                left join cus_history_product$tbl_postfix as h on p.id =h.product_id and datediff(h.date_created,'$todate')=0 where 
                p.id = $id GROUP BY p.id ";
               // echo $sqlnew; die();
        $arrnew =  $objDbSelect->GetArray($sqlnew); 
                if(count($arrnew)>0){
                    $num_out = $arrnew[0]['num_out']==''?0:$arrnew[0]['num_out'];
                    $num_in = $arrnew[0]['num_in']==''?0:$arrnew[0]['num_in'];
                    $num_start = $arrnew[0]['num_start'];
                    $num_end = $num_start + $num_in-$num_out;
                    $arrnew[0]['num_end']= $num_end;
                    $arrnew[0]['date_sum']= $todate;
                    
                }
                $arr[count($arr)]= $arrnew[0]; 
       }
       
               
    }else{
        
                $sqlnew = "SELECT name_vi as name,p.quanlity as num_start,p.id,sum(num) as num_out  , (select sum(i.quanlity)  
                from  cus_importproduct as i
                where i.product_id = p.id and datediff(i.date_created,'$todate')=0 and service_id = $hotel_id)  as num_in FROM cus_product$tbl_postfix as p
                left join cus_history_product$tbl_postfix as h on p.id =h.product_id and datediff(h.date_created,'$todate')=0  
                where p.id = $id  group by p.id"; 
                $arr =  $objDbSelect->GetArray($sqlnew); 
                //echo '<pre>'; print_r($arr); die();  
                if(count($arr)>0){
                    $num_out = strlen($arr[0]['num_out'])==0||$arr[0]['num_out']==0?0:$arr[0]['num_out'];
                    $num_in = strlen($arr[0]['num_in'])==0||$arr[0]['num_in']==0?0:$arr[0]['num_in'];
                    $arr[0]['num_in'] = $num_in;
                    $arr[0]['num_out'] = $num_out;
                    $num_start = $arr[0]['num_start'];
                    $num_end = $num_start + $num_in-$num_out;
                    $arr[0]['num_end']= $num_end;
                    $arr[0]['date_sum']= $todate;
                    
                }
                
               // echo '<pre>'; print_r($arr); die();
        
    }
    return $arr;
     
}


function frmhistoryinventory_loadGrid($fromdate,$todate){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = frmhistoryinventory_getGrid($fromdate,$todate);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/cus_historyinventory_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmhistoryinventory_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmhistoryinventory_tbl',true);");
    //$objResponse->addScriptCall("dataTableObj.CreatDataTable('frmhistoryinventory_tbl',true);");// có sắp xếp theo cột
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  frmhistoryinventory_getGrid($fromdate,$todate){
    global $objDbSelect,$lang,$tbl_postfix,$user_id,$hotel_id;
    $arr=array();
    $now = date('Y-m-d'); 
    if($fromdate==$now){
        $sql="  SELECT name_vi as name,p.quanlity as num_start,p.id,sum(num) as num_out  , (select sum(i.quanlity)  
                from  cus_importproduct as i
                where i.product_id = p.id and datediff(i.date_created,'$todate')=0 and service_id = $hotel_id)  as num_in FROM cus_product$tbl_postfix as p
                left join cus_history_product$tbl_postfix as h on p.id =h.product_id and datediff(h.date_created,'$todate')=0
                where p.checkquanlity = 1   group by p.id  "; 
        $arr = $objDbSelect->GetArray($sql); 
    }else{        
        $sqlSelect = " SELECT name_vi as name,product_id as id,num_start,sum(num_in) as num_in,sum(num_out) as num_out 
                   FROM (select num_start,num_in,product_id,num_out from  cus_summary_product  where datediff('$fromdate',date_sum)<=0 
                   and datediff('$todate',date_sum)>=0 and service_id = '$hotel_id' order by date_sum asc ) as s 
                    join cus_product$tbl_postfix as p on s.product_id = p.id  and p.checkquanlity = 1 group by product_id";
                    //echo $sqlSelect; die();
        $arr = $objDbSelect->GetArray($sqlSelect);
        $countsum =  count($arr);
        if($now <= $todate){
            $sql_out=" SELECT p.quanlity as num_start,p.id,sum(num) as num_out  FROM cus_product$tbl_postfix as p
                left join cus_history_product$tbl_postfix as h on p.id =h.product_id and datediff(h.date_created,'$now')=0
                where p.checkquanlity = 1 group by p.id ;";
                //echo  $sql_out; die();
                
                $arrnow_out = $objDbSelect->GetArray($sql_out);
                
                $arr_out = array();
                for($i=0;$i<count($arrnow_out);$i++){
                    $id = $arrnow_out[$i]['id'];
                    $num_out = $arrnow_out[$i]['num_out']==''?0:$arrnow_out[$i]['num_out'];
                    $arr_out[$id]= $num_out;
                }   
                       
                $sql_in="SELECT p.id,sum(h.quanlity) as num_in FROM cus_product$tbl_postfix as p
                join cus_importproduct as h on p.id =h.product_id and datediff(h.date_created,'$now')=0 and h.service_id = $hotel_id
                where p.checkquanlity = 1 group by p.id ;";
                $arrnow_in = $objDbSelect->GetArray($sql_in);
                $arr_in = array(); 
                for($i=0;$i<count($arrnow_in);$i++){
                    $id = $arrnow_in[$i]['id'];
                    $num_in = $arrnow_in[$i]['num_in'];
                    $arr_in[$id]= $num_in;
                }
                
                 for($i=0;$i<$countsum;$i++){
                    $id = $arr[$i]['id'];
                    $num_out = isset($arr_out[$id])?$arr_out[$id]:0;
                    unset($arr_out[$id]);
                    $num_in = isset($arr_in[$id])?$arr_in[$id]:0;
                    $arr[$i]['num_in']+=$num_in; 
                    $arr[$i]['num_out']+=$num_out; 
                }
                // echo '<pre>'; print_r($arr);die() ;
                $strIdnew='-1';   
                //echo '<pre>'; print_r($arr_out);die();              
                 foreach($arr_out as $key=>$value){
                     $strIdnew.=','.$key;
                 }
                 $count = count($arr);
                 if($strIdnew!='-1'){
                   $sqlnew="SELECT p.id,p.name_vi as name,p.quanlity as num_start,sum(num) as num_out  , (select sum(i.quanlity)  from  cus_importproduct as i
                            where i.product_id = p.id and datediff(i.date_created,now())=0 and i.service_id = $hotel_id)  as num_in FROM cus_product$tbl_postfix as p
                            left join cus_history_product$tbl_postfix as h on p.id =h.product_id and datediff(h.date_created,now())=0
                            where p.checkquanlity = 1 and p.id IN ($strIdnew) group by p.id ";
                           // echo $sqlnew
                   $arrnew =  $objDbSelect->GetArray($sqlnew);                   
                   for($i=0;$i<count($arrnew);$i++){
                      $arr[$count+$i]=$arrnew[$i]; 
                   }
                 }
             
        }         
    }
    for($i=0;$i<count($arr);$i++){
        $id = $arr[$i]['id'];
        $num_start=$arr[$i]['num_start'];         
        $num_in=$arr[$i]['num_in']==''?0:$arr[$i]['num_in']; 
        $num_out=$arr[$i]['num_out']==''?0:$arr[$i]['num_out']; 
        $arr[$i]['num_last']+=$num_start+$num_in-$num_out;
        $arr[$i]['from_date'] = $fromdate;
        $arr[$i]['to_date'] = $todate;
        $arr[$i]['num_in']=$num_in;
        $arr[$i]['num_out']=$num_out;
    }
   // echo '<pre>'; print_r($arr);die();

    return  $arr;  
    
} 
# end region process data
?>
